home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / kermit / e / xxu.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-17  |  3.8 KB  |  112 lines

  1. /*  X X U  --  20-to-Unix filename converter  */
  2.  
  3. /*
  4.  Change DEC-20 or VAX/VMS style filenames into normal Unix names.
  5.  Handy for use after ftp MGETs, when you find your directory full of
  6.  files with names like LIB:<KERMIT>CKUFIO.C.2 or FRED::[ETHEL]A.B;37
  7.  when all you really wanted was ckufio.c and a.b.
  8.  
  9.  Usage: xxu file(s)
  10.  
  11.  Action: Renames argument files as follows:
  12.    strips Unix path name from front (up to rightmost '/') if present
  13.    strips DEC device:, node:: names from front (up to rightmost ':') if present
  14.    strips DEC-20 <directory> or VMS [directory] name if present
  15.    strips DEC-20 version number from end (everything after 2nd dot) if present
  16.    strips VMS generation number from end (everything after ';') if present
  17.    lowercases any uppercase letters
  18.    honors DEC-20 CTRL-V quote for special characters
  19.    discards unquoted unprintable characters
  20.    if result is null, file is renamed to xxfile-n, where n is a number.
  21.    if result would write over an existing file, file also renamed to xxfile-n.
  22.  
  23.  Recommended procedure: make a new directory, cd to it, then FTP files
  24.  from DEC-20 or VMS system, then do "xxu *".
  25.  
  26.  Author:  F. da Cruz, CUCCA, July 85
  27. */
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <sys/file.h>            /* For access() */
  31.  
  32. char name[500];                /* File name buffer */
  33. char *pp, *cp, *xp;            /* Character pointers */
  34. char delim;                /* Directory Delimiter */
  35. int dc = 0, n = 0;            /* Counters */
  36. int quote = 0, indir = 0; done = 0;    /* Flags */
  37.  
  38. main(argc,argv) int argc; char **argv; {
  39.  
  40.     if (argc < 2) {            /* Give message if no args */
  41.     fprintf(stderr,"Usage: xxu file(s)\n");
  42.     exit(1);
  43.     }
  44.     n = 0;                /* Unfixable filename counter */
  45.     while (--argc > 0) {        /* For all files on command line... */
  46.     argv++;
  47.     xp = *argv;            /* Copy pointer for simplicity */
  48.     printf("%s ",*argv);        /* Echo name of this file */
  49.  
  50.     pp = name;            /* Point to translation buffer */
  51.     *name = '\0';            /* Initialize buffer */
  52.     dc = 0;                /* Filename dot counter */
  53.     done = 0;            /* Flag for early completion */
  54.  
  55.     for (cp = xp; (*cp != '\0') && !done; cp++) { /* Loop thru chars... */
  56.  
  57.         if (quote) {        /* If this char quoted, */
  58.         *pp++ = *cp;        /*  include it literally. */
  59.         quote = 0;
  60.         }
  61.         else if (indir) {        /* If in directory name, */
  62.         if (*cp == delim) indir = 0; /* look for end delimiter. */
  63.         }
  64.         else switch (*cp) {
  65.         case '<':        /* Discard DEC-20 directory name */
  66.             indir = 1;
  67.             delim = '>';
  68.             break;
  69.         case '[':        /* Discard VMS directory name */
  70.             indir = 1;
  71.             delim = ']';
  72.             break;
  73.         case '/':        /* Discard Unix path name */
  74.         case ':':               /*  or DEC dev: or node:: name */
  75.             pp = name; 
  76.             break;
  77.         case '.':        /* DEC -20 generation number */
  78.                 if (++dc == 1)    /* Keep first dot */
  79.                 *pp++ = *cp;
  80.             else        /* Discard everything starting */
  81.                 done = 1;    /* with second dot. */
  82.             break;
  83.         case ';':        /* VMS generation or DEC-20 attrib */
  84.             done = 1;        /* Discard everything starting with */
  85.             break;        /* semicolon */
  86.             case '\026':        /* Control-V quote for special chars */
  87.             quote = 1;        /* Set flag for next time. */
  88.             break;
  89.         default:
  90.             if (isupper(*cp))      /* Uppercase letter to lowercase */
  91.                     *pp++ = tolower(*cp);
  92.             else if (isprint(*cp)) /* Other printable, just keep */
  93.                 *pp++ = *cp;
  94.         }
  95.     }
  96.     *pp = '\0';            /* Done with name, terminate it */
  97.     if (strcmp(name,xp) == 0) {    /* If no renaming necessary, */
  98.         printf("(ok)\n");        /*  just give message. */
  99.         continue;
  100.         }
  101.     while (*name == '\0' || access(name,0) == 0) { /* Find unique name */
  102.         sprintf(name,"xxfile-%d",n++);
  103.     }
  104.     printf("=> %s ",name);        /* Tell what new name will be */
  105.     if (rename(xp,name) == 0)    /* Try to rename it */
  106.         printf("(ok)\n");        /* Say what happened */
  107.     else
  108.         perror("failed");
  109.     }
  110.     exit(0);                /* Done. */
  111. }
  112.